<?PHP
class content_access
{
	
	var $data;
	var $input;
	var $output;
	var $where;
	var $access;
	var $mode = 'noisy';
	var $status;
	var $mailmode;
	function access_mailer()
	{
		$mailer =& new emailer;
		global $conf;


		if($this->mailmode=='grant')
		{
	
			$activate_url=$conf->absolute_path."/user/activate/".$this->code;

			$message='Greetings from '.$conf->site_name." <br />";
		 	$message.='You can now watch: URCC 18 Relentless PPV '."<br />"." <br />";
			$message.='Until: 14 November 8pm (+8gmt)'."<br />"." <br />";
			//$message.='Here: '."<br />"." <br />";
			$message.='-----------------------------------------------------------------------'." <br />";
	
			$message.='Thank you for purchasing at pinoytuner.com'." <br />";

			$mailer->mail_message=$message;
			$mailer->mail_subject='You can now watch URCC 18 Relentless PPV';
			$mailer->mail_to=$this->input['user_email'];
			$mailer->send_mail('quick');

		}
	}
	function content_access()
	{
	
	}	
	
	function check_permissions()
	{
		$where=process_input($this->where);
	
		$this->data=get_sql('content_access','id',$where,NULL);
	
	}
	
	function grant_token()
	{
		global $conf;
		$c_content = new content();
		$c_content->where['id']= $_SESSION['content'];
		$c_content->get_content();
		$c_content->get_content_addons();
		$content=$c_content->data;
		if($content['access_end_type']=='date')
		{
		
				$this->input['expiry']=$content['access_end'];
		}
		else
		{
					$addition=$content['access_expiry'] * 3600 + time();
				$expiry_data = date("Y-m-d H:i:s", $addition); 
				$this->input['expiry']=$expiry_data ;
		}
		$this->input['hash']=randomchar();
		$this->input['sandbox']=$conf->a['purchase-sandbox'];

	
		process_sql_post($this->input,'access_token','add',NULL,NULL);
		$this->mailmode='grant';

		$this->access_mailer();
	}

	
	function check_token()
	{
		$this->access=false;
		global $conf;
		$where=process_input($this->where);
			$w[0]='user_email';
			$w[1]=$_SESSION['user_email'];
			$w[2]='=';
			
			$where[]=$w;
			
		//	print_r($where);
		//print_r($_SESSION['user_email']);
		$this->data=get_sql('access_token','id',$where,NULL);
		$v=current($this->data);
		//print_r($this->data);
		$current_date = date("Y-m-d H:i:s", time()); 
		
		if(empty($this->data))
		{
			$this->status='tokenpresent';
		}
		else
		{
			$this->status='notoken';
		}
			
			if($this->status='tokenpresent')
			{
					foreach($this->data as $x)
					{
					
						if($current_date <= $x['expiry'])
						{
							mp('Not expired');
							$tokens[]=$x;
						}
						else
						{
							mp('expired');

						}
					}
					if(empty($tokens))
					{
						if($this->mode=='noisy')
						{
							$conf->errors[]='You have no subscription or your subscription has expired';
						}
							
							$this->access=false;
					}
					else
					{
						$this->access=true;
						mp('allow');
					}

		
				
			}
			else
			{
				//$conf->messages[]='Access Denied';
			}
			
			
		
	}
}
?>